package com.jz.subclass.conroller;

import com.jz.legalplantclass.entity.LegalPlantClassAddVo;
import com.jz.legalplantclass.entity.LegalPlantClassUpdateStatusVo;
import com.jz.legalplantclass.entity.LegalPlantClassUpdateVo;
import com.jz.legalplantclass.service.ILegalplantclassService;
import com.jz.planclass.model.*;
import com.jz.subclass.model.*;
import com.jz.subclass.service.SubClassService;
import com.jz.subclass.test.LegalPersonEnum;
import com.jz.subclass.test.PlanStatusEnum;
import com.jz.subclass.test.SubStatusEnum;
import com.jz.util.ResultVo;
import com.jz.util.ResultBuildVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by WISE on 2021/12/26.
 */
@RestController
@CrossOrigin//开启跨域
public class SubClassController {

    ///1.下拉框
    @PostMapping("metadata/getPullDownList")
    public ResultVo subDroplist() {
        List<StatusVo> statusVoList = new ArrayList<>();
       /* StatusVo statusVo1 = new StatusVo(1, "启用");
        StatusVo statusVo2 = new StatusVo(0, "禁用");
        statusVoList.add(statusVo1);
        statusVoList.add(statusVo2);*/
        //枚举类可以遍历
        for (SubStatusEnum subStatusEnum : SubStatusEnum.values()) {
            StatusVo statusVo = new StatusVo(subStatusEnum.getStatus(), subStatusEnum.getStatusName());
            statusVoList.add(statusVo);
        }
        return ResultBuildVo.success(statusVoList);


    }

    //1.首页分页
    @Autowired
    private SubClassService subClassService;

    @PostMapping("subClass/queryPage")
    public ResultVo firstPage(@RequestBody SubClassSelectVo subClassSelectVo) {
        ResultVo resultVo = subClassService.firstPage(subClassSelectVo);

        return resultVo;
    }

    //2.新增
    @PostMapping("subClass/add")
    @Transactional
    public ResultVo add(@RequestBody @Valid SubClassAddVo subClassAddVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultBuildVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        ResultVo resultVo = subClassService.addSubClass(subClassAddVO);
        return resultVo;
    }


    //3.修改
    @GetMapping("queryById")
    @Transactional
    public ResultVo queryById(Integer id) {

        if (id == null) {
            return ResultBuildVo.error("ID不能为空");
        }
        return subClassService.queryById(id);
    }


    @PostMapping("subClass/update")
    public ResultVo update(@RequestBody @Valid SubClassUpdateVo subClassUpdateVo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultBuildVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return subClassService.updateSubClass(subClassUpdateVo);
    }

    @PostMapping("updateStatus")
    public ResultVo updateStatus(@RequestBody @Valid SubClassUpdateStatusVo subClassUpdateStatusVo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultBuildVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return subClassService.updateStatus(subClassUpdateStatusVo);
    }

    //---------------计划大类plancalss---------
    //1.下拉框
//    @PostMapping("metadata/getPullDownList")
//    public ResultVo pullDownList() {
//        List<PlanstatusVO> planClassVoList = new ArrayList<>();
//        for (PlanStatusEnum planStatusEnum: PlanStatusEnum.values()){

//            PlanstatusVO planstatusVO =new PlanstatusVO(planStatusEnum.getStatus(),planStatusEnum.getStatusaName());
//                planClassVoList.add(planstatusVO);
//        }
//
//        return ResultBuildVo.success(planClassVoList);
//
//    }

  /*  @Autowired
    private PlanClassService planClassService;

    @PostMapping("queryPage")
    public ResultVo queryPage(@RequestBody @Valid PlanClassSelectVo planClassSelectVo,BindingResult bindingResult) {

      if (bindingResult.hasErrors()){
          return ResultBuildVo.error(bindingResult.getFieldError().getDefaultMessage());
      }
        ResultVo resultVo = planClassService.queryPage(planClassSelectVo);
        return resultVo;
    }
    @PostMapping("add")
    public ResultVo add(@RequestBody PlanClassAddVo planClassAddVo) {
        ResultVo resultVo = planClassService.addPlanClass(planClassAddVo);
        return resultVo;
    }

    @GetMapping("queryById")
    public ResultVo queryById2(Integer id) {
        if (id == null){
            return ResultBuildVo.error("id不能为空");
        }

        return planClassService.queryById(id);

    }

    @PostMapping("update1")
    public ResultVo update1(@RequestBody @Valid PlanClassUpdateVo planClassUpdateVo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultBuildVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return planClassService.updatePlanClass(planClassUpdateVo);
    }

    @PostMapping("updateStatus1")
    public ResultVo updateStatus1(@RequestBody @Valid PlanClassUpdateStatusVo planClassUpdateStatusVo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultBuildVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return planClassService.updateStatus(planClassUpdateStatusVo);
    }*/


    //--------------3.法人工厂--------

//    //1.法人下拉框
//    @GetMapping("legalPerson/ofCurrentUser")
//    public ResultVo ofCurrentUser() {
//
////        List<LegalPersonNameVO> legalPersonNameVOList = new ArrayList<>();
//        Integer userId = 1;
//        List<LegalPersonNameVO> legalPersonNameVOList = legalpersonclassService.queryLegalPersonName(userId);
//
//        //        for (LegalPersonEnum legalPersonEnum : LegalPersonEnum.values()) {
////            LegalPersonNameVO legalPersonNameVO = new LegalPersonNameVO(legalPersonEnum.getLegalPersonCode(), legalPersonEnum.getLegalPersonName());
////            legalPersonNameVOList.add(legalPersonNameVO);
////        }
//        return ResultBuildVo.success(legalPersonNameVOList);
//
//    }
//
//    @Autowired
//    private ILegalpersonclassService legalpersonclassService;
//
//    //    //2.分页功能
//    @PostMapping("legalPlant/query")
//    public ResultVo limitPage(@RequestBody LegalPersonSelectClass legalPersonSelectClass) {
//
//        return legalpersonclassService.limitPage(legalPersonSelectClass);
//
//    }

    //3.添加功能
    @PostMapping("legalPlant/add")
    @Transactional
    public ResultVo addPlant(@RequestBody @Valid LegalPlantClassAddVo legalPlantClassAddVo, BindingResult bindingResult) {

        if (bindingResult.hasErrors()) {
            return ResultBuildVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        ResultVo resultVo = subClassService.addPlantClass(legalPlantClassAddVo);

        return resultVo;
    }
    //3.修改功能
    @PostMapping("legalPlant/update")
    @Transactional
    public ResultVo updatePlant(@RequestBody  @Valid LegalPlantClassUpdateVo legalPlantClassUpdateVo, BindingResult bindingResult){

        if (bindingResult.hasErrors()) {
            return ResultBuildVo.error(bindingResult.getFieldError().getDefaultMessage());
        }

        return legalplantclassService.updateLegalPlantClass(legalPlantClassUpdateVo);
    }

    @Autowired
    private ILegalplantclassService legalplantclassService;
    @PostMapping("legalPlant/updatestatus")
    public ResultVo legalPlantUpdateStatus(@RequestBody @Valid LegalPlantClassUpdateStatusVo legalPlantClassUpdateStatusVo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultBuildVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return legalplantclassService.updateLegalPlantStatus(legalPlantClassUpdateStatusVo);
    }
}



